package com.example.admin.mybledemo.ota;

/**
 * OTA常量定义
 * 从OtaManager中提取，统一管理所有常量
 */
public final class OtaConstants {
    
    private OtaConstants() {
        // 防止实例化
    }
    
    // 服务UUID - V1版本
    public static final String APP_OTA_SERVICE_UID = "5265616C-6D70-6761-2D49-6E73696465AA";
    public static final String APP_OTA_WRITE_UID = "5265616C-6D71-6761-2D49-6E73696465AA";
    public static final String APP_OTA_VERSION_UID = "5265616C-6D72-6761-2D49-6E73696465AA";
    public static final String APP_OTA_NOTIFY = "5265616C-6D73-6761-2D49-6E73696465AA";
    
    // 服务UUID - V2版本
    public static final String APP_OTA_SERVICE_UID_V2 = "5265616C-6D80-6761-2D49-6E73696465AA";
    public static final String APP_OTA_WRITE_UID_V2 = "5265616C-6D81-6761-2D49-6E73696465AA";
    public static final String APP_OTA_VERSION_UID_V2 = "5265616C-6D82-6761-2D49-6E73696465AA";
    public static final String APP_OTA_NOTIFY_V2 = "5265616C-6D83-6761-2D49-6E73696465AA";
    
    // Boot模式服务UUID
    public static final String BOOT_OTA_SERVICE_UID = "5265616C-6D65-6761-2D49-6E73696465AA";
    public static final String BOOT_OTA_CMD_UID = "5265616C-6D66-6761-2D49-6E73696465AA";
    public static final String BOOT_OTA_ASK_UID = "5265616C-6D67-6761-2D49-6E73696465AA";
    
    // 错误码定义
    public static final int ASK_STATE_SUCC = 0;          // 成功
    public static final int ASK_STATE_FAIL_MAC = 1;      // MAC地址验证失败
    public static final int ASK_STATE_FAIL_CODE = 2;     // 凭证码验证失败
    public static final int ASK_STATE_UNKNOW = 3;        // 未知错误
    public static final int ASK_STATE_NOT_EXPECT = 4;    // 非预期状态
    public static final int ASK_STATE_FCS_ERROR = 5;     // FCS校验失败
    public static final int ASK_STATE_ADDR_INVALID = 6;  // 地址无效
    public static final int ASK_STATE_CREDIT_ERROR = 7;  // 凭证错误
    public static final int ASK_STATE_VERSION_ERROR = 8; // 版本不兼容
    
    // 新增错误码
    public static final int ERROR_ROM_MISMATCH = 100;    // ROM版本不匹配
    public static final int ERROR_VERSION_TOO_OLD = 101; // 升级版本太旧
    public static final int ERROR_VERSION_FORMAT = 102;  // 版本号格式错误
    public static final int ERROR_ROM_CHECK_FAILED = 103; // ROM校验失败
    
    // 协议命令
    public static final byte[] UPDATE_CMD = {0x63, 0x69, 0x6d, 0x72};
    public static final byte[] VERSION_REQ_CMD = {0x01, 0x00};
    public static final byte[] OTA_START_CMD = {0x01, 0x01};
    public static final byte[] OTA_BLOCK_CMD = {0x01, 0x04};
    public static final byte[] OTA_END_CMD = {0x01, 0x03};
    
    // 数据配置
    public static final int DATA_SIZE = 32;
    public static final int SERVICE_DISCOVERY_TIMEOUT = 5000;

    // 重试延迟配置 (毫秒)
    public static final int CONNECT_RETRY_DELAY = 2000;
    public static final int VERSION_CHECK_RETRY_DELAY = 1000;
    public static final int CREDENTIAL_CHECK_RETRY_DELAY = 100;
    public static final int DATA_TRANSFER_RETRY_DELAY = 500;
    public static final int VERIFICATION_RETRY_DELAY = 1000;

    // 最大重试次数
    public static final int MAX_RETRY_COUNT = 10; // 增加重试次数，与Flutter端保持一致
    public static final int VERSION_CHECK_RETRY = 2;
    public static final int CREDENTIAL_CHECK_RETRY = 2;
    public static final int DATA_TRANSFER_RETRY = 5;

    // 超时配置
    public static final int CONNECT_TIMEOUT = 8000; // 与Flutter端保持一致：8秒超时
    public static final int WRITE_TIMEOUT = 10000;
    public static final int BLOCK_DELAY = 50;
    
    /**
     * 获取错误描述
     */
    public static String getErrorDescription(int errorCode) {
        switch (errorCode) {
            case ASK_STATE_SUCC: return "成功";
            case ASK_STATE_FAIL_MAC: return "MAC地址验证失败";
            case ASK_STATE_FAIL_CODE: return "凭证码验证失败";
            case ASK_STATE_UNKNOW: return "未知错误";
            case ASK_STATE_NOT_EXPECT: return "非预期状态";
            case ASK_STATE_FCS_ERROR: return "FCS校验失败";
            case ASK_STATE_ADDR_INVALID: return "地址无效";
            case ASK_STATE_CREDIT_ERROR: return "凭证错误";
            case ASK_STATE_VERSION_ERROR: return "版本不兼容";
            case ERROR_ROM_MISMATCH: return "ROM版本不匹配";
            case ERROR_VERSION_TOO_OLD: return "升级版本太旧";
            case ERROR_VERSION_FORMAT: return "版本号格式错误";
            case ERROR_ROM_CHECK_FAILED: return "ROM校验失败";
            default: return "未知错误码: " + errorCode;
        }
    }
    
    /**
     * 获取服务UUID（根据协议版本）
     */
    public static String getServiceUuid(ProtocolVersion version) {
        return version == ProtocolVersion.V2 ? APP_OTA_SERVICE_UID_V2 : APP_OTA_SERVICE_UID;
    }
    
    /**
     * 获取写入特征UUID（根据协议版本）
     */
    public static String getWriteUuid(ProtocolVersion version) {
        return version == ProtocolVersion.V2 ? APP_OTA_WRITE_UID_V2 : APP_OTA_WRITE_UID;
    }
    
    /**
     * 获取版本特征UUID（根据协议版本）
     */
    public static String getVersionUuid(ProtocolVersion version) {
        return version == ProtocolVersion.V2 ? APP_OTA_VERSION_UID_V2 : APP_OTA_VERSION_UID;
    }
    
    /**
     * 获取通知特征UUID（根据协议版本）
     */
    public static String getNotifyUuid(ProtocolVersion version) {
        return version == ProtocolVersion.V2 ? APP_OTA_NOTIFY_V2 : APP_OTA_NOTIFY;
    }
    
    /**
     * 复制命令字节数组（防止外部修改原数组）
     */
    public static byte[] getUpdateCommand() {
        return UPDATE_CMD.clone();
    }
    
    public static byte[] getVersionRequestCommand() {
        return VERSION_REQ_CMD.clone();
    }
    
    public static byte[] getOtaStartCommand() {
        return OTA_START_CMD.clone();
    }
    
    public static byte[] getOtaBlockCommand() {
        return OTA_BLOCK_CMD.clone();
    }
    
    public static byte[] getOtaEndCommand() {
        return OTA_END_CMD.clone();
    }
} 